﻿function showSwitch(username){
    $('#readyusername').html("当前用户："+username);
    $('#login_panel').css('display','none');
	$('#switch_panel').css('display','');
}
function showLogin(){
    $('#login_panel').css('display','');
	$('#switch_panel').css('display','none');
}
$(function(){
    chrome.extension.sendRequest({cmd: "connected",data:"0"}, function(response) {
        if(response.result.ret==true){
		    showSwitch(response.result.user);
		}
		else{
		    chrome.extension.sendRequest({cmd: "connectresume",data:"0"}, function(response) {
			    if(response.result.ret==false){
				    chrome.extension.sendRequest({cmd: "reconnect",data:"0"}, function(response) {
					    if(response.result.ret==false){
						    showLogin();
						}
						else{
						    showSwitch(response.result.user);
						}
					});
				}
				else{
				    showSwitch(response.result.user);
				}
			});
		}
    });
	$('#signin_switch').click(function(){
	    showLogin();
	});
	$('#taobao_account').click(function(){
	    chrome.extension.sendRequest({cmd: "popthirdlogin",data:"0"}, function(response) {
		});
	});
	$('#signin').submit(function(){
	    var aForm=new Object();
	    aForm.username=new Object();
        aForm.password=new Object();
		aForm.remember=new Object();
        aForm.username.value=$('#username').attr('value');
        aForm.password.value=$('#password').attr('value');
		aForm.remember.value=$('#remember').attr('value');
	    chrome.extension.sendRequest({cmd: "login",data:aForm}, function(response) {
		    $('#result').html(response.ret);
		});
		return false;
	});
});